//
//  BitMapAndMaskController.m
//  Core Graphics Code
//
//  Created by liboxiang on 2019/1/14.
//  Copyright © 2019 liboxiang. All rights reserved.
//

#import "BitMapAndMaskController.h"

@interface BitMapAndMaskController ()

@end

@implementation BitMapAndMaskController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    UIImage *oneImage = [UIImage imageNamed:@"one.png"];
    UIImage *twoImage = [UIImage imageNamed:@"two.png"];
//    CGContextClipToMask(context, CGRectMake(50, 100, 200, 200), oneImage.CGImage);
//    [twoImage drawInRect:CGRectMake(50, 100, 200, 200)];
    
    CGImageRef maskRef = oneImage.CGImage;
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);
    CGContextClipToMask(context, CGRectMake(50, 100, 200, 200), mask);
    [twoImage drawInRect:CGRectMake(50, 100, 200, 200)];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(mask);
    self.view.layer.contents = (id)image.CGImage;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
